﻿
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models.Common;
using Models.JsonResultModel;
using ServiceWeb.DBGateway;
using UtilsBase;
using UtilsORM;
using UtilsORM.MySqlData;
using GatewayServer.Common;

namespace GatewayServer.Controllers
{
    public class TaskTimeFixedController : BaseMagerController
    {
        EFYihongIotContext ef = new EFYihongIotContext();
        // GET: Mager_Department
        public IActionResult list(IFormCollection FC, HttpReqModel httpRequest, iot_product reqProduct, int PageIndex = 1, int PageSize = 15, string type = "1")
        {
            List<SugarParameter> sugarParameters = new List<SugarParameter>();
            int totalRecord = 0;
            string sqlwhere = "";



            if (!string.IsNullOrEmpty(reqProduct.p_guid))
            {
                sqlwhere = sqlwhere + "  and tf_p_guid = @tf_p_guid ";
                sugarParameters.Add(new SugarParameter("tf_p_guid", reqProduct.p_guid));
            }
            else
            {
                sqlwhere = sqlwhere + "    and tf_is_template=0 ";
            }

            if (!string.IsNullOrEmpty(httpRequest.name))
            {
                sqlwhere = sqlwhere + " and tf_title like CONCAT('%',@name,'%') ";
                sugarParameters.Add(new SugarParameter("name", httpRequest.name));

            }
            if (!string.IsNullOrEmpty(FC["hid_pageIndex"]))
            {
                PageIndex = int.Parse(FC["hid_pageIndex"].ToString());

            }

            if (!string.IsNullOrEmpty(httpRequest.status))
            {
                //设置状态
                sqlwhere = sqlwhere + " and tf_status=@status";
                sugarParameters.Add(new SugarParameter("status", httpRequest.status));
            }


            string sql = "select *,'' taskname,'' tasktype from iot_device_task_time_fixed   where 1= 1  " + sqlwhere;
            DataTable dt = MySqlYihongIot.getPageDataTable(PageIndex, PageSize, sql, sugarParameters, "tf_id desc", ref totalRecord);
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["tf_task_type"].ToString() == "device")
                {
                    dr["tasktype"] = "设备指令";
                    string deviceGuid = dr["tf_task_guid"].ToString();
                    iot_device device = new DBIotDevice().getDevice(deviceGuid);
                    if (device != null)
                    {
                        dr["taskname"] = device.d_code + "-" + device.d_name;
                    }
                }
                else if (dr["tf_task_type"].ToString() == "taskplain")
                {
                    dr["tasktype"] = "流程任务";

                    string guid = dr["tf_task_guid"].ToString();
                    iot_device_task_plain model = new DBIotDeviceTaskPlain().getPlain(guid);
                    if (model != null)
                    {
                        dr["taskname"] = model.tp_title;
                    }
                }
            }

            ViewData["DataList"] = dt;

            ViewBag.PageSize = PageSize;
            ViewBag.RecordCount = totalRecord;//总条数
            ViewBag.PageCount = totalRecord % PageSize == 0 ? totalRecord / PageSize : totalRecord / PageSize + 1;//分的页数
            ViewBag.PageIndex = PageIndex;//当前页码
            return View(httpRequest);
        }


        public IActionResult edit(HttpReqModel httpReqModel, iot_product reqProduct)
        {
            ViewBag.tf_p_guid = reqProduct.p_guid;
            if (httpReqModel.order == "upd")
            {
                ViewBag.guid = httpReqModel.guid.ToString();
            }
            else
            {
                ViewBag.guid = Guid.NewGuid().ToString();
            }
            ViewBag.order = httpReqModel.order;
            return View();
        }

        public IActionResult editPlain(HttpReqModel httpReqModel, iot_product reqProduct)
        {
            ViewBag.tf_p_guid = reqProduct.p_guid;
            if (httpReqModel.order == "upd")
            {
                ViewBag.guid = httpReqModel.guid.ToString();
            }
            else
            {
                ViewBag.guid = Guid.NewGuid().ToString();
            }
            ViewBag.order = httpReqModel.order;
            return View();
        }


        public RespResultData saveAll(IFormCollection FC, HttpReqModel httpRequest, iot_device_task_time_fixed req, iot_product reqProduct)
        {
            req.tf_c_guid = LoginUserInfo.userCompanyGuid;

            new DBIotDeviceTaskTimeFixed().saveAll(req);
            return RespResult.Sucess();
        }

        public RespResultData getDeviceList(HttpReqModel httpRequest, iot_product product)
        {
            if (!string.IsNullOrEmpty(product.p_guid))
            {
                return RespResult.Sucess(new DBIotDevice().getDeviceByProductList(product.p_guid));
            }
            else
            {
                return RespResult.Sucess(new DBIotDevice().getDeviceByNoProductList());
            }

        }

        public RespResultData getPlainList(HttpReqModel httpRequest, iot_product product)
        {
            if (!string.IsNullOrEmpty(product.p_guid))
            {
                return RespResult.Sucess(new DBIotDeviceTaskPlain().getPlainByProductList(product.p_guid));
            }
            else
            {
                return RespResult.Sucess(new DBIotDeviceTaskPlain().getPlainByNotProductList());
            }
            // return RespResult.Sucess(new ServiceIotDeviceTaskPlain().getPlainList(LoginUserInfo.userCompanyGuid));
        }
        public RespResultData getCommandList(HttpReqModel httpRequest)
        {
            iot_device device = new DBIotDevice().getDevice(httpRequest.guid);

            return RespResult.Sucess(new DBIotDeviceCommand().getCommandList(device.d_model_guid));
        }
        public RespResultData getModel(HttpReqModel httpRequest)
        {
            return RespResult.Sucess(new DBIotDeviceTaskTimeFixed().getModel(httpRequest.guid));
        }

        public RespResultData changeStatus(HttpReqModel httpReqModel)
        {
            new DBIotDeviceTaskTimeFixed().changeStatus(httpReqModel.guid);
            return RespResult.Sucess();
        }
        public RespResultData del(HttpReqModel httpReqModel)
        {
            new DBIotDeviceTaskTimeFixed().del(httpReqModel.guid);
            return RespResult.Sucess();
        }
    }
}